package com.jin.core;

import com.jin.model.User;

import java.util.HashMap;
import java.util.Map;

//在项目的任意为位置获取用户的id值
public class LocalUser {
    //接收一个map的数据
    //threadLocal好用的地方：1.解决了线程安全的问题
    private static ThreadLocal<Map<String,Object>> threadLocal = new ThreadLocal<>();
    //把当前用户传入到user里面去
    public static void set(User user,Integer scope){
        Map<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("scope",scope);
        //把map保存到threadLocal里面去
        LocalUser.threadLocal.set(map);
    }

    //清除线程的资源
    public static void clear(){
        LocalUser.threadLocal.remove();
    }

    public static User getUser(){
        Map<String, Object> map = LocalUser.threadLocal.get();
        Object user = map.get("user");
        return (User) user;
    }

    public static Integer getScope(){
        Map<String, Object> map = LocalUser.threadLocal.get();
        Object scope = map.get("scope");
        return (Integer) scope;
    }
}
